<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_SESSION['payresult']){
	$_RQ['op']='pay_result';
}
$this->model('auction')->checklogin();
$return=array();
if($_RQ['op']=='main'){
	$return['item']=array(
		'payType'=>'recharge',
		'purpose'=>'pledge',
	);
	set_html('title','会员充值');
	exi($return);
}
if($_RQ['op']=='post_mobile' || $_RQ['op']=='post_windows'){
	$post=get_postdata();
	if(!$post['money']){
		exi('请先设置充值金额！','error');
	}
	$fee=sprintf("%.2f", $post['money']);
	$data=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'fee'=>$fee,'createtime'=>TIMESTAMP);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	
	do{
		$tid='JYA'.TIMESTAMP.random(7);
		$old=pdo_getcolumn('core_paylog',array('id'=>$tid),'id');
	}while($old);
	
	$pay_log=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'id'=>$tid,
		'oid'=>$id,
		'status'=>0,
		'createtime'=>TIMESTAMP,
		'title'=>'余额充值',
		'no_type'=>ijson_encode(array('hdfk','credit')),
		'fee'=>$fee,
		'plugin'=>'jy_jdpm',
		'action'=>'member.payment',
		'more'=>'',
		'back_url'=>'jy_jdpm/member.payment/pay_result',
	);
	pdo_insert('core_paylog',$pay_log);
	$pay_id=pdo_insertid();
	$params=array(
		'title'=>$pay_log['title'],
		'fee'=>$pay_log['fee'],
		'id'=>$pay_log['id']
	);
	$setting=uni_setting('pay');
	if($setting['wechat']==1){
		$wechat=$setting;
	}else{
		//借用系统支付
		exi('该公众号暂不支持微信支付！','error');
		//获取粉丝openid
		
	}
	if(!$wechat['wechat_mchid'] || !$wechat['wechat_miyue']){
		exi('支付出错：微信支付参数设置错误！','error');
	}
	if($_RQ['op']=='post_windows'){
		$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
		$post=array(
			'appid'=>trim($setting['wechat_appid']),
			'mch_id'=>trim($setting['wechat_mchid']),
			'device_info'=>'WEB',//设备名称
			'nonce_str'=>random(32),//随机字符串
			'body'=>$pay_log['title'],//浏览器打开的网站主页title名 -商品概述
			'out_trade_no'=>$pay_log['id'],//订单号
			'total_fee'=>$pay_log['fee']*100,//单位分
			'attach'=>$_SESSION['uniacid'],//自定义参数
			'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],//当前客户端ip
			'notify_url'=>SITEROOT.'custom/payresult/wechat.php',//回调域名
			'trade_type'=>'NATIVE',//扫码支付为原生
			'product_id'=>$pay_id,//产品编号
		);
		ksort($post);
		$str='';
		foreach($post as $key=>$value){
			$str.=$key.'='.$value.'&';
		}
		$stringSignTemp=$str.'key='.trim($setting['wechat_miyue']);
		$sign=strtoupper(md5($stringSignTemp));
		$post['sign']=$sign;
		$return=cfc('http')->ihttp_request($url,array2xml($post));
		$code=xml2array($return['content']);
		$codeurl=$code['code_url'];
		if(!$codeurl){
			exi('生成支付二维码失败,请联系开发者检查微信支付参数！'.ijson_encode($code),'error');
		}
		exi($codeurl);
	}else{
		$wechat_options=cfc('pay')->wechat_build($params,$wechat);
		if(is_error($wechat_options)){
			exi('支付出错：'.$wechat_options['message'],'error');
		}
		exi($wechat_options);
	}
	
}

if($_RQ['op']=='pay_result'){
	$recharge=pdo_get('core_recharge',array('id'=>$_SESSION['payresult']['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('id','fee','status'));
	if($recharge['fee']==$_SESSION['payresult']['fee']){
		if($recharge['status']){
			exi('充值成功','','goback');
		}
		if($_SESSION['payresult']['status']==1){
			pdo_update('core_recharge',array('status'=>1,'pay_type'=>$_SESSION['payresult']['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
			//cfc('core')->member_credit($_SESSION['uid'],'credit2',$recharge['fee'],1,'member','会员自助充值');
			pdo_update('jy_jdpm_member',array('wallet_pledge +='=>$recharge['fee']),array('mid'=>$_SESSION['uid']));
			$pay_id = $_SESSION['payresult']['pay_id'];
			unset($_SESSION['payresult']);
			$log=array(
				'mid'=>$_SESSION['uid'],
				'pid'=>0,
				'type'=>0,
				'money'=>$recharge['fee'],
				'channel'=>1,
				'create_time'=>TIMESTAMP,
				'remarks'=>'微信充值',
				'order_no'=>$pay_id
			);
			pdo_insert('jy_jdpm_money_log',$log);
			exi('充值成功！','','goback');
		}else{
			unset($_SESSION['payresult']);
			exi('充值失败！','','goback');
		}
	}else{
		unset($_SESSION['payresult']);
		exi('支付失败，支付金额不匹配！','','goback');
	}
}
